<?php
namespace Admin\Controller;
class WorkController extends AdminController{
    //分类列表
    public function type(){
        $result = $this->lists('work_type', array(), 'state asc');
        $this->assign('result', $result);
        $this->display();
    }
    //添加分类
    public function typeAdd() {
        if(IS_POST) {
            $data['type'] = I('type');
            $data['state'] = I('state');
            $result = M('work_type')->add($data);
            if ($result) {
                $this->success('添加成功', U('type'));
            } else {
                $this->error('添加失败', U('type'));
            }
        } else {
            $this->display('typeAdd');
        }
    }
    //修改分类
    public function typeEdit() {
        $id = I('id');
        if (IS_POST) {
            $data['type'] = I('type');
            $data['state'] = I('state');
            $result = M('work_type')->where('id='.$id)->save($data);
            if ($result) {
                $this->success('修改成功', U('type'));
            } else {
                $this->error('修改失败', U('type'));
            }
        } else {
            $result = M('work_type')->where('id='.$id)->find();
            $this->assign('result', $result);
            $this->display('typeEdit');
        }
    }
    //删除分类
    public function typeDelete() {
        $id = I('id');
        $result = M('work_type')->where('id='.$id)->delete();
        if ($result) {
            $this->success('删除成功', U('type'));
        } else {
            $this->error('删除失败', U('type'));
        }
    }
    //工作名称列表
    public function name() {
        $tid = I('tid');
        $result = $this->lists('work_name', array('tid'=>$tid));
        $type = M('work_type')->where('id='.$tid)->find();
        $this->assign('result', $result);
        $this->assign('type', $type);
        $this->display('name');
    }
    //添加工作名称
    public function nameAdd() {
        $data['tid'] = I('tid');
        if (IS_POST) {
            $data['tid'] = I('tid');
            $data['name'] = I('name');
            $result = M('work_name')->add($data);
            if ($result) {
                $this->success('添加成功', U('name', array('tid'=>$data['tid'])));
            } else {
                $this->error('添加失败', U('name', array('tid'=>$data['tid'])));
            }
        } else {
            $types = M('work_type')->select();
            $this->assign('tid', $data['tid']);
            $this->assign('types', $types);
            $this->display('nameAdd');
        }
    }
    //编辑工作名称
    public function nameEdit() {
        $id = I('id');
        $data['tid'] = I('tid');
        if (IS_POST) {
            $data['name'] = I('name');
            $result = M('work_name')->where('id='.$id)->save($data);
            if ($result) {
                $this->success('修改成功', U('name', array('tid'=>$data['tid'])));
            } else {
                $this->error('修改失败', U('name', array('tid'=>$data['tid'])));
            }
        } else {
            $types = M('work_type')->select();
            $this->assign('types', $types);
            $result = M('work_name')->where('id='.$id)->find();
            $this->assign('result', $result);
            $this->assign('tid', $tid);
            $this->display('nameEdit');
        }
    }
    //删除工作名称
    public function nameDelete() {
        $id = I('id');
        $tid = I('tid');
        $result = M('work_name')->where('id='.$id)->delete();
        if ($result) {
            $this->success('删除成功', U('name', array('tid'=>$tid)));
        } else {
            $this->error('删除失败', U('name', array('tid'=>$tid)));
        }
    }
}